import { type FormInstance, showSuccessToast } from 'vant';
import { ref } from 'vue';

import { getDefaultPlatform } from '@/api';
import { deserialize } from '@/plugins/reflect-metadata';
import type { ListState } from '@/types';

import {
  getPlatformRate,
  getPlatformTypeList,
  syncCardProduct,
  updatePlatformDefault,
  updatePlatformRate,
} from '../api';
import { PlatformConfig, PlatformTypeItem, PlatformTypeList } from '../models/PlatformConfig';

const useAppConfg = () => {
  const statePlatform = ref<ListState<PlatformTypeItem>>({
    data: [],
    loading: false,
    total: 0,
  });

  const platformRef = ref<FormInstance>({} as FormInstance);
  const params = ref<PlatformConfig>(new PlatformConfig());
  const platformMap = ref<Record<string, string>>({});

  const fetchSyncList = async () => {
    try {
      statePlatform.value.loading = true;
      const { data } = await getPlatformTypeList();
      const resultData = deserialize(data, PlatformTypeList);
      statePlatform.value.data = resultData.items;
      statePlatform.value.total = resultData.total;
      platformMap.value = resultData.platformMap;
    } catch (error) {
      //
    } finally {
      statePlatform.value.loading = false;
    }
  };

  const fetchCurPlat = async () => {
    try {
      statePlatform.value.loading = true;
      const { data } = await getDefaultPlatform();
      params.value.platformType = data.data || data.msg;
    } catch (error) {
      //
    } finally {
      statePlatform.value.loading = false;
    }
  };

  const syncLoading = ref(false);
  const handleSync = async () => {
    try {
      syncLoading.value = true;
      const { data } = await syncCardProduct(params.value);
      showSuccessToast(data.msg || '同步成功');
    } catch (error) {
      //
    } finally {
      syncLoading.value = false;
    }
  };

  const fetchRate = async () => {
    try {
      statePlatform.value.loading = true;
      const { data } = await getPlatformRate();
      params.value.rate = data.data;
    } catch (error) {
      //
    } finally {
      statePlatform.value.loading = false;
    }
  };

  const platformLoading = ref(false);
  const handlePlatform = async () => {
    try {
      platformLoading.value = true;
      const { data } = await updatePlatformRate(params.value);
      showSuccessToast(data.msg || '修改成功');
    } catch (error) {
      //
    } finally {
      platformLoading.value = false;
    }
  };

  const platformDefaultLoading = ref(false);
  const handlePlatformDefault = async () => {
    try {
      platformDefaultLoading.value = true;
      const { data } = await updatePlatformDefault(params.value);
      showSuccessToast(data.msg || '修改成功');
    } catch (error) {
      //
    } finally {
      platformDefaultLoading.value = false;
    }
  };

  return {
    fetchRate,
    fetchSyncList,
    fetchCurPlat,
    platformRef,

    statePlatform,
    platformMap,
    params,
    syncLoading,
    platformLoading,
    platformDefaultLoading,
    handleSync,
    handlePlatform,
    handlePlatformDefault,
  };
};

export default useAppConfg;
